home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg1.cab / HighlightText.java < prev    next >
Text File  |  2005-05-18  |  8KB  |  224 lines

  1. import com.sun.star.uno.UnoRuntime;
  2. import com.sun.star.script.provider.XScriptContext;
  3. import com.sun.star.lang.XMultiComponentFactory;
  4. import com.sun.star.lang.EventObject;
  5. import com.sun.star.uno.Type;
  6. import com.sun.star.uno.AnyConverter;
  7. import com.sun.star.text.XTextDocument;
  8. import com.sun.star.beans.PropertyValue;
  9. import com.sun.star.script.XLibraryContainer;
  10. import com.sun.star.awt.*;
  11. import com.sun.star.util.*;
  12.  
  13. import java.awt.Color;
  14.  
  15. public class HighlightText implements com.sun.star.awt.XActionListener {
  16.  
  17.     // UNO awt components of the Highlight dialog
  18.     XDialog findDialog = null;
  19.     XTextComponent findTextBox;
  20.  
  21.     // The document being searched
  22.     XTextDocument theDocument;
  23.  
  24.     // The text to be searched for
  25.     private String searchKey = "";
  26.  
  27.     public void showForm(XScriptContext context) {
  28.         System.err.println("Starting showForm");
  29.  
  30.         XMultiComponentFactory xmcf =
  31.             context.getComponentContext().getServiceManager();
  32.  
  33.         Object[] args = new Object[1];
  34.         args[0] = context.getDocument();
  35.  
  36.         Object obj;
  37.         try {
  38.             obj = xmcf.createInstanceWithArgumentsAndContext(
  39.                 "com.sun.star.awt.DialogProvider", args,
  40.                 context.getComponentContext());
  41.         }
  42.         catch (com.sun.star.uno.Exception e) {
  43.             System.err.println("Error getting DialogProvider object");
  44.             return;
  45.         }
  46.  
  47.         XDialogProvider xDialogProvider = (XDialogProvider)
  48.             UnoRuntime.queryInterface(XDialogProvider.class, obj);
  49.  
  50.         System.err.println("Got DialogProvider, now get dialog");
  51.  
  52.         try {
  53.             findDialog = xDialogProvider.createDialog(
  54.                 "vnd.sun.star.script:" +
  55.                 "ScriptBindingLibrary.Highlight?location=application");
  56.         }
  57.         catch (java.lang.Exception e) {
  58.             System.err.println("Got exception on first creating dialog: " +
  59.                 e.getMessage());
  60.         }
  61.  
  62.         if (findDialog == null) {
  63.             if (tryLoadingLibrary(xmcf, context, "Dialog") == false ||
  64.                 tryLoadingLibrary(xmcf, context, "Script") == false)
  65.             {
  66.                 System.err.println("Error loading ScriptBindingLibrary");
  67.                 return;
  68.             }
  69.             try {
  70.                 findDialog = xDialogProvider.createDialog(
  71.                     "vnd.sun.star.script://" +
  72.                     "ScriptBindingLibrary.Highlight?location=application");
  73.             }
  74.             catch (com.sun.star.lang.IllegalArgumentException iae) {
  75.                 System.err.println("Error loading ScriptBindingLibrary");
  76.                 return;
  77.             }
  78.         }
  79.  
  80.         XControlContainer controls = (XControlContainer)
  81.             UnoRuntime.queryInterface(XControlContainer.class, findDialog);
  82.  
  83.         XButton highlightButton = (XButton) UnoRuntime.queryInterface(
  84.             XButton.class, controls.getControl("HighlightButton"));
  85.         highlightButton.setActionCommand("Highlight");
  86.  
  87.         findTextBox = (XTextComponent) UnoRuntime.queryInterface(
  88.             XTextComponent.class, controls.getControl("HighlightTextField"));
  89.  
  90.         XButton exitButton = (XButton) UnoRuntime.queryInterface(
  91.             XButton.class, controls.getControl("ExitButton"));
  92.         exitButton.setActionCommand("Exit");
  93.  
  94.         theDocument = (XTextDocument) UnoRuntime.queryInterface(
  95.             XTextDocument.class, context.getDocument());
  96.  
  97.         highlightButton.addActionListener(this);
  98.         exitButton.addActionListener(this);
  99.  
  100.         findDialog.execute();
  101.  
  102.         return;
  103.     }
  104.  
  105.     public void actionPerformed(ActionEvent e) {
  106.         if (e.ActionCommand.equals("Exit")) {
  107.             findDialog.endExecute();
  108.             return;
  109.         }
  110.         else if (e.ActionCommand.equals("Highlight")) {
  111.             searchKey = findTextBox.getText();
  112.  
  113.             // highlight the text in red
  114.             Color cRed = new Color(255, 0, 0);
  115.             int red = cRed.getRGB();
  116.             
  117.             XReplaceable replaceable = (XReplaceable)
  118.                 UnoRuntime.queryInterface(XReplaceable.class, theDocument); 
  119.  
  120.             XReplaceDescriptor descriptor =
  121.                 (XReplaceDescriptor) replaceable.createReplaceDescriptor();
  122.  
  123.             // Gets a XPropertyReplace object for altering the properties
  124.             // of the replaced text
  125.             XPropertyReplace xPropertyReplace = (XPropertyReplace)
  126.                 UnoRuntime.queryInterface(XPropertyReplace.class, descriptor);
  127.  
  128.             // Sets the replaced text property fontweight value to Bold
  129.             PropertyValue wv = new PropertyValue("CharWeight", -1,
  130.                 new Float(com.sun.star.awt.FontWeight.BOLD),
  131.                     com.sun.star.beans.PropertyState.DIRECT_VALUE);
  132.  
  133.             // Sets the replaced text property color value to RGB parameter
  134.             PropertyValue cv = new PropertyValue("CharColor", -1,
  135.                 new Integer(red),
  136.                     com.sun.star.beans.PropertyState.DIRECT_VALUE);
  137.  
  138.             // Apply the properties
  139.             PropertyValue[] props = new PropertyValue[] { cv, wv }; 
  140.  
  141.             try {
  142.                 xPropertyReplace.setReplaceAttributes(props);
  143.  
  144.                 // Only matches whole words and case sensitive
  145.                 descriptor.setPropertyValue(
  146.                     "SearchCaseSensitive", new Boolean(true));
  147.                 descriptor.setPropertyValue("SearchWords", new Boolean(true));
  148.             }
  149.             catch (com.sun.star.beans.UnknownPropertyException upe) {
  150.                 System.err.println("Error setting up search properties");
  151.                 return;
  152.             }
  153.             catch (com.sun.star.beans.PropertyVetoException pve) {
  154.                 System.err.println("Error setting up search properties");
  155.                 return;
  156.             }
  157.             catch (com.sun.star.lang.WrappedTargetException wte) {
  158.                 System.err.println("Error setting up search properties");
  159.                 return;
  160.             }
  161.             catch (com.sun.star.lang.IllegalArgumentException iae) {
  162.                 System.err.println("Error setting up search properties");
  163.                 return;
  164.             }
  165.  
  166.             // Replaces all instances of searchKey with new Text properties
  167.             // and gets the number of instances of the searchKey 
  168.             descriptor.setSearchString(searchKey); 
  169.             descriptor.setReplaceString(searchKey); 
  170.             replaceable.replaceAll(descriptor);
  171.         }
  172.     }
  173.  
  174.     public void disposing(EventObject o)
  175.     {
  176.         // do nothing
  177.     }
  178.  
  179.     private boolean tryLoadingLibrary(
  180.         XMultiComponentFactory xmcf, XScriptContext context, String name)
  181.     {
  182.         System.err.println("Try to load ScriptBindingLibrary");
  183.  
  184.         try {
  185.             Object obj = xmcf.createInstanceWithContext(
  186.                "com.sun.star.script.Application" + name + "LibraryContainer",
  187.                context.getComponentContext());
  188.  
  189.             XLibraryContainer xLibraryContainer = (XLibraryContainer)
  190.                 UnoRuntime.queryInterface(XLibraryContainer.class, obj);
  191.  
  192.             System.err.println("Got XLibraryContainer");
  193.  
  194.             Object serviceObj = context.getComponentContext().getValueByName(
  195.                 "/singletons/com.sun.star.util.theMacroExpander");
  196.                                                                                 
  197.             XMacroExpander xme = (XMacroExpander) AnyConverter.toObject(
  198.                 new Type(XMacroExpander.class), serviceObj);
  199.                                                                                 
  200.             String bootstrapName = "bootstraprc";
  201.             if (System.getProperty("os.name").startsWith("Windows")) {
  202.                 bootstrapName = "bootstrap.ini";
  203.             }
  204.  
  205.             String libURL = xme.expandMacros(
  206.                 "${$SYSBINDIR/" + bootstrapName + "::BaseInstallation}" +
  207.                 "/share/basic/ScriptBindingLibrary/" +
  208.                 name.toLowerCase() + ".xlb/");
  209.  
  210.             System.err.println("libURL is: " + libURL);
  211.  
  212.             xLibraryContainer.createLibraryLink(
  213.                 "ScriptBindingLibrary", libURL, false);
  214.  
  215.             System.err.println("liblink created");
  216.  
  217.         } catch (com.sun.star.uno.Exception e) {
  218.             System.err.println("Got an exception loading lib: " + e.getMessage());
  219.             return false;
  220.         }
  221.         return true;
  222.     }
  223. }
  224.